//logs.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({
  data: {
    dataList: [],
    isSelectedAll: false,
    totalPrice: "0.00",
    totalCount: 0,
    postId:"",
  },
  onLoad: function (options) {
    this.data.postId = options.postId;

    this.getListData();
  },
  getListData() {
    var _this = this;
    var map = {};
    api.getRequestData(app.bfGoodsUrl, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        var list = res.data.model;
        for (var i = 0; i < list.length; i++) {
          var dict = list[i];
          dict.isSelected = 0;
          dict.selectCount = 1;
        }
        console.log(JSON.stringify(list[0]));
        _this.data.dataList = list;
        _this.setData({
          dataList: _this.data.dataList
        });
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  goToDetail(e) {
    var id = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: '/pages/index/goodsDetail/goodsDetail?productId=' + id + "&page=bf",
    })
  },
  subCountClick(e) {
    var index = parseInt(e.currentTarget.dataset.key);
    var dict = this.data.dataList[index];
    var count = parseInt(dict.selectCount);
    if (count - 1 <= 0) {
      count = 1;
      app.alert("购买数量不能是0呀~");
    } else {
      count -= 1;
    }
    dict.selectCount = count;
    this.setData({
      dataList: this.data.dataList
    });
    this.showTaotalPrice();
  },
  addCountClick(e) {
    var index = parseInt(e.currentTarget.dataset.key);
    var dict = this.data.dataList[index];
    var count = parseInt(dict.selectCount);
    if (count + 1 >= 100) {
      count = 99;
      app.alert("购买数量不能超过99呀~");
    } else {
      count += 1;
    }
    dict.selectCount = count;
    this.setData({
      dataList: this.data.dataList
    });
    this.showTaotalPrice();
  },
  rowSelectedClick(e) {
    var index = parseInt(e.currentTarget.dataset.key);
    var dict = this.data.dataList[index];
    if (dict.isSelected == 1) {
      dict.isSelected = 0;
    } else {
      dict.isSelected = 1;
    }
    console.log(dict.isSelected);
    this.setData({
      dataList: this.data.dataList
    });
    this.showTaotalPrice();
  },
  allClick() {
    if (this.data.isSelectedAll) {
      this.data.isSelectedAll = false;
    } else {
      this.data.isSelectedAll = true;
    }
    for (var i = 0; i < this.data.dataList.length; i++) {
      var dict = this.data.dataList[i];
      if (this.data.isSelectedAll) {
        dict.isSelected = 1;
      } else {
        dict.isSelected = 0;
      }
    }
    this.setData({
      isSelectedAll: this.data.isSelectedAll,
      dataList: this.data.dataList
    });
    this.showTaotalPrice();
  },
  showTaotalPrice(e) {
    var map = this.getTotalPrice();
    this.setData({
      totalPrice: map.totalPrice,
      totalCount: map.totalCount
    });
  },
  getTotalPrice() {
    var totalPrice = 0;
    var totalCount = 0;
    for (var i = 0; i < this.data.dataList.length; i++) {
      var dict = this.data.dataList[i];
      if (dict.isSelected == 1) {
        var count = parseInt(dict.selectCount + "");
        var pirce = parseFloat(dict.salePrice + "");
        totalPrice += count * pirce;
        totalCount += count;
      }
    }
    var map = {};
    map.totalPrice = totalPrice.toFixed(2);
    map.totalCount = totalCount;
    return map;
  },
  goToCreatePage() {
    if (this.data.totalCount == 0) {
      app.alert("请选中要结算的商品~");
      return;
    }
    var list = [];
    for (var i = 0; i < this.data.dataList.length; i++) {
      var dic = this.data.dataList[i];
      if (dic.isSelected == 1) {
        list.push(dic);
      }
    }
    var listString = JSON.stringify(list);
    wx.setStorageSync("payList", listString);
    var url = '/pages/index/createOrder/createOrder';
    if (this.data.postId.length > 0){
      url = '/pages/index/createOrder/createOrder?postId=' + this.data.postId;
    }
    wx.navigateTo({
      url: url,
    })
  },

})
